package top.tagao.m01_structuralPatterns.s_01_proxyDesign.proxy_02_dynamic.dy_01_jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/26 18:57
 * 实现接口InvocationHandler 重写invoke方法，处理执行代码逻辑
 */
public class CustomizeHandle implements InvocationHandler {
    private Object tager;

    public CustomizeHandle(Object tager) {
        this.tager = tager;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        System.out.println("proxy class :" + proxy.getClass());
        System.out.println("arg : "+ Arrays.toString(args));
        System.out.println("method : "+method.getName());

        Object result = method.invoke(tager, args);
        after();
        return result;
    }

    private void before() {
        System.out.println("代理类 前期工作 handle before");
    }

    private void after() {
        System.out.println("代理类 后期工作 handler after");
    }
}
